Imports System.Windows.Forms
Public Class IrcPlugin
    Implements Core.Plugin
    Private _resources As Core.Resources
    Private _connection As IrcConnection
    Private WithEvents toolbar As ToolBar
    Public Function getPluginInfo() As Core.PluginInfo Implements Core.Plugin.getPluginInfo

    End Function

    Public Sub load(ByRef Resources As Core.Resources) Implements Core.Plugin.load
        _connection = New IrcConnection
        _resources = Resources
        _resources.PluginManager.registerForNotify(Me, "Startup")
    End Sub

    Public Sub Process(ByRef notifiableEvent As Core.NotifiableEvent, ByVal args As Core.Package) Implements Core.Plugin.Process
        If notifiableEvent.getEventType = "Startup" Then
            Dim MainForm As Form = _resources.Application.MainForm
            For Each control As Windows.Forms.Control In MainForm.Controls
                If control.Name = "ToolContainer" Then
                    CreateTools(control)
                End If
            Next
        End If
    End Sub

    Public Sub CreateTools(ByRef ToolContainer As ToolStripContainer)
        toolbar = New ToolBar()
        toolbar.Buttons.AddRange(getButtons())
        ToolContainer.ContentPanel.Controls.Add(toolbar)
    End Sub

    Private Sub toolbar_ButtonClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles toolbar.ButtonClick
        If e.Button.Name = "newConnection" Then
            Dim dlgNewConnection As New NewConnection()
            If dlgNewConnection.ShowDialog = DialogResult.OK Then
                Dim ip As Net.IPAddress = Net.Dns.GetHostAddresses(dlgNewConnection.Server)(0)
                _connection = _resources.ConnectionPool.requestConnection(ip, dlgNewConnection.Port, New IrcConnectionFactory())
                _connection.Nick = dlgNewConnection.Nick
                _connection.Username = dlgNewConnection.Username
                Dim window As New ChatWindow(_connection)
                window.MdiParent = _resources.Application.MainForm
                window.Show()
            End If
        End If
    End Sub

    Function getButtons() As ToolBarButton()
        Dim buttons(0) As ToolBarButton
        buttons(0) = New ToolBarButton("Nueva Conexion")
        buttons(0).Name = "newConnection"
        Return buttons
    End Function

    Public Sub unload() Implements Core.Plugin.unload
        _connection.close()
    End Sub
End Class
